<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style></style>
    <style>
        *{
            list-style: none;
            margin: 0px;
            padding: 0px;
            
        }
        .bigwrap{
            width: 700px;
            height: 400px;
            background-color: bisque;
            margin: 100px auto 0px;
            display: flex;
            overflow-x: hidden;
            /* overflow: auto; */
        }
        li{
            width: 200px;
            height: 250px;
            background-color: brown;
            margin: 30px;
            float: left;
            flex-shrink: 0;
        }
        .scroll{
            width: 700px;
            border: 1px solid black;
            height: 10px;
            margin:0px auto;
            position: relative;
            margin-top: 20px;
            border-radius: 10px;
        }
        .scroll>div{
            width: 200px;
            height: 100%;
            background-color: black;
            position: absolute;
            left: 0px;
            top: 0;
            border-radius: 10px;
        }
        .scroll>div:hover{
            height: 20px;
            top: -5px;
        }
        .lastli{
            margin-right: 0px;
            background-color: coral;
        }
        .li1{
            background-color: cornflowerblue;
        }
        .li2{
            background-color: darkgoldenrod;
        }
        .li3{
            background-color: darkmagenta;
        }
        .li4{
            background-color: darkturquoise;
        }
    </style>
</head>
<body>
    <div class="bigwrap">
        <li class="li1">

        </li>
        <li class="li2">

        </li >
        <li class="li3">

        </li >
        <li class="li4">

        </li>
        <li class="li5">

        </li>
        <li class="li6">

        </li>
        <li class="lastli">

        </li>
    </div>
    <div class="scroll" >
        <div></div>
    </div>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    const bigwrap=$(".bigwrap");//列表
    const li=$(".bigwrap li");//列表子项目
    const scr=$(".scroll>div");//滚动条
    const scrdad=$(".scroll");//滚动条框
    scr[0].flag=0;
    //计算滚动条滑块长度,并设置
    let listwidth=0;
    let nolastwidth=0;
    li.each(function(i,v){
        listwidth+=$(v).outerWidth(true);
        if(i!=li.length-1) nolastwidth+=$(v).outerWidth(true);
    });
    let scrwidth=scrdad.width()*bigwrap.width()/listwidth;
    scr.width(scrwidth);
    scrdad.on('mousedown',function(ev){

        if(ev.pageX<=scr.offset().left){
            //点击滑块移动到相应位置 左边
            scr.animate({"left":ev.pageX-scrdad.offset().left},"fast");

            /*这里设置列表的滚动条移动距离，需要滚动条距离左边的属性*/
        }
        else if(ev.pageX>=scr.offset().left+scr.width()){
            
            if((ev.pageX-scrdad.offset().left)*(listwidth/bigwrap.width())>nolastwidth){
                //如果右边剩余位置小于一个li宽度，则滑块移到最右边
                // scr.css({"left":scrdad.width()-scr.width()});
                scr.animate({"left":scrdad.width()-scr.width()},"fast");

                /*这里设置列表的滚动条移动距离，需要滚动条距离左边的属性*/
            }
            else{
                //点击滑块移动到相应位置 右边
                scr.animate({"left":ev.pageX-scrdad.offset().left-scr.width()},"fast");
                
                /*这里设置列表的滚动条移动距离，需要滚动条距离左边的属性*/
            }
        }
    });
    scr.on('mousedown',function(ev){
        scr[0].flag=1;
        let downx=ev.pageX;
        let scrleft=parseInt(scr.css("left"));
        console.log(scr.offset());
        $(document).on('mousemove',function(ev){
            if(scr[0].flag==1){
                let movex=scrleft+ev.pageX-downx;
                bigwrap.scrollLeft(movex*(listwidth/bigwrap.width()));
                scr.css({"left":movex});
                //设置黑块移动范围
                if(parseInt(scr.css("left"))<=0){
                    scr.css({"left":0});
                }
                if(parseInt(scr.css("left"))>=scrdad.width()-scr.width()){
                    scr.css({"left":scrdad.width()-scr.width()});
                }
            }
        });
        $(document).on('mouseup',function(){
            scr[0].flag=0;
        });
    });
</script>